var chargingApp = angular.module('chargingApp', ['chargingApp.chargingService']);


var chargePortArray = {};
chargePortArray['1'] = 'A:';
chargePortArray['2'] = 'B:';
chargePortArray['3'] = 'C:';

chargingApp.filter('getValueInStaticFilter', function() {
  return function(input, staticArrayName) {
    var value = eval(staticArrayName + "[input]");
    if (!!value) {
      return value;
    }
    return "";
  };
});
chargingApp.controller('chargingController', function($scope, chargingService, $timeout, $filter, $interval) {
  function formatId(id) {
    if (!!!id) {
      return '';
    }
    return id.slice(0, 3) + "****" + id.slice(id.length - 4);
    // return id.replace(/(\d{3})\d{9}(\d{4})/, "$1****$2");
  }


  $scope.initView = function() {
    chargingService.getBigScreenByChargerId().then(function(data) {
      // if (!!!data || !!!data.pojo) {
      //   alert("查询的桩ID不存在，请重新查询！");
      //   window.location = "search.html";
      //   return;
      // }
      console.log(data);
      $scope.chargingObj = data.pojo;
      $scope.chargingstate = data.pojo.stateList;
      console.log($scope.chargingObj.stateList[0]);
      console.log($scope.chargingObj.stateList[1]);
      // if($scope.chargingObj.charger_v==null && $scope.chargingObj.charger_i==null && $scope.chargingObj.charger_p==null){
      //   $scope.chargingObj.charger_v = "_";
      //   $scope.chargingObj.charger_p = "_";
      //   $scope.chargingObj.charger_i = "_"; 
      // }
      if (!!$scope.chargingObj.stateList && $scope.chargingObj.stateList.length > 2) {
        $scope.chargingObj.stateList.length = 2;
      }
      $timeout(function() {
        angular.forEach($scope.chargingObj.stateList, function(pileStatus, index) {
          if (!!pileStatus.cardId) {
            pileStatus.cardId = formatId(pileStatus.cardId);
          }
          if (!!pileStatus.user_pay_card) {
            pileStatus.user_pay_card = formatId(pileStatus.user_pay_card);
          }
          pileStatus.charger_kwh = !!pileStatus.charger_kwh ? $filter('number')(pileStatus.charger_kwh, 1) + ' 度' : '-';
          pileStatus.remaining_time = !!pileStatus.remaining_time ? pileStatus.remaining_time + ' 分钟' : '-';
          pileStatus.battery_temperature = !!pileStatus.battery_temperature ? $filter('number')(pileStatus.battery_temperature, 1) + ' 度' : '-';
          if (2 == pileStatus.charge_status) {
            var soc = !!pileStatus.soc ? pileStatus.soc : 0;
            // console.log($('#circle' + pileStatus.port_no));
            $('#circle' + (index + 1)).circleProgress({
              value: soc / 100,
              startAngle: -1.5,
              size: 700,
              emptyFill: 'rgba(0, 0, 0, .3)',
            }).on('circle-animation-progress', function(event, progress) {
              $(this).find('strong').html(parseInt(soc * progress) + '%');
            });
            // console.log($('#circlehidden' + pileStatus.port_no));
            $('#circlehidden' + (index + 1)).circleProgress({
              value: soc / 100,
              startAngle: -1.5,
              size: 750,
              animationStartValue: 0,
              emptyFill: '#343738',
              fill: {
                color: 'rgba(255, 255, 255, .0)'
              }
            }).on('circle-animation-progress', function(event, progress) {
              // $(this).find('strong').html(parseInt(soc * progress) + '%');
            });
          }
        });
      });
    });
  }
        $scope.getWeatherByCityId = function(){
      chargingService.getWeatherByCityId().then(function(data){
      $scope.weatherList = data.pojo;
    })
  };
  $scope.getWeatherByCityId();
  $scope.initView();
  // var count = 0;
  // $interval(function() {
  //   count++;
  //   if (count == 1 || count % 3 == 0) {
  //     $scope.initView();
  //   }
  // }, 20000);
  // $interval(function(){
  //   $scope.initView()
  // },10000);
});